package com.atguigu.lease.web.admin.controller.login;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.atguigu.lease.common.login.LoginUser;
import com.atguigu.lease.common.login.LoginUserHolder;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import utils.JwtUtil;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class LoginController {
    private final LoginService loginService;
    private final UserInfoService userInfoService;
    private final SystemUserService systemUserService;
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        CaptchaVo captchaVo=loginService.CaptchaVo();
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String res=loginService.login(loginVo);
        return Result.ok(res);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info() {
        //Claims claims = JwtUtil.parseToken(token);
        Long userId = LoginUserHolder.getLoginUser().getUserId();

        LambdaQueryWrapper<SystemUser> l=new LambdaQueryWrapper<>();
        l.eq(SystemUser::getId,userId);
        SystemUser one = systemUserService.getOne(l);

        SystemUserInfoVo v=new SystemUserInfoVo(one.getName(),one.getAvatarUrl());
        return Result.ok(v);
    }
}